package xx.pro.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Classname Demo3
 * @Description TODO
 * @Date 2023/11/4 14:02
 * @Created by wangxianlin
 */
public class Demo6 {

    static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行");
        // 没有返回值
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> {
            System.out.println("任务1 子线程执行" + Thread.currentThread().getName());
        },fixedThreadPool);


        // 有返回值
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2 子线程执行" + Thread.currentThread().getName());
            return 11;
        },fixedThreadPool);

        future1.runAfterBothAsync(future2,()->{
            System.out.println("任务3 "+ Thread.currentThread().getName());
        },fixedThreadPool);

        System.out.println(future2.get());
        fixedThreadPool.shutdown();
    }
}
